﻿using FluentValidation;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Components.Forms;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace BBCore.Core.Models.Samples
{
    public class ZDL_HYJ_MASTER_HEAD
    {
        [Column(IsPrimary = true)]
        [Display(Name = "HEAD_ID")]
        public int ZDL_BKC_JOB_HEAD_ID { get; set; }
        [NotNull]
        [Display(Name = "JOB_NO")]
        [Required(ErrorMessage = "JOB_NO不可为空")]
        public string? JOB_NO { get; set; }
        [Display(Name = "数量")]
        public int QUANTITY { get; set; }
        [Display(Name = "线别(L)")]
        [Required(ErrorMessage = "线别不可为空")]
        public string? LINE_CODE { get; set; }
        [Display(Name = "需求日期")]
        public DateTime REQ_DATE { get; set; }=DateTime.Now;
        [Display(Name = "组织")]
        [Required(ErrorMessage = "组织")]
        public int ORG_ID { get; set; }

        
        //导航属性，OneToMany
        [Navigate(nameof(ZDL_HYJ_DETAIL_BODY.ZDL_BKC_JOB_BODY_ID))]
        //public virtual List<ZDL_HYJ_DETAIL_BODY>? ZBJB_ITEMS { get; set; } = new List<ZDL_HYJ_DETAIL_BODY>();
        //[ValidateComplexType]
        public virtual List<ZDL_HYJ_DETAIL_BODY>? ZBJB_ITEMS { get; set; } = [];

        /*
        嵌套模型、集合类型和复杂类型
        若要验证绑定模型的整个对象图（包括集合类型和复杂类型的属性），请使用试验性
        Microsoft.AspNetCore.Components.DataAnnotations.Validation 包提供的 ObjectGraphDataAnnotationsValidator
        [ValidateComplexType]
        例如:
        public class ProductDetails
        {
        [Required(ErrorMessage = "Description is required")]
        public string Description { get; set; }
        }
        public class Product
        {
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }
        [Required(ErrorMessage = "Supplier is required")]
        public string Supplier { get; set; }
        [ValidateComplexType]
        public ProductDetails ProductDetails { get; set; }
        }
        */
    }
    public class ZDL_HYJ_MASTER_HEADValidator : AbstractValidator<ZDL_HYJ_MASTER_HEAD>
    {
        public ZDL_HYJ_MASTER_HEADValidator()
        {
            RuleFor(x=>x.JOB_NO).NotEmpty();
            RuleFor(x => x.LINE_CODE).NotEmpty();
            RuleFor(x => x.ORG_ID).NotEmpty();
            RuleFor(x => x.QUANTITY).GreaterThan(0);//数量必须大于0
            RuleForEach(x => x.ZBJB_ITEMS).SetValidator(new ZDL_HYJ_DETAIL_BODYValidator());
        }
        

    }
}
